package com.china08.yxyapi.api.home.index.news;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNews;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNewsRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--咨询
 * 
 */
@CrossOrigin
@RestController
public class IndexSrhNewsController extends YxyApiBaseController {

    @Autowired
    private YxyHomeNewsRepository yxyHomeNewsRepository;

    @ApiOperation(value = "首页咨询")
    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/news", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;
        List<RespModel> respModelLst = new ArrayList<RespModel>();

        if (StringUtils.isNotBlank(schoolId)) //
        {
            // 获取6条数据
            List<YxyHomeNews> yxyHomeSliderLst = yxyHomeNewsRepository
                    .findTop6ByEdbIdAndStatusOrderByCreatedDateDesc(schoolId, 1);
            for (YxyHomeNews yxyHomeNews : yxyHomeSliderLst) //
            {
                RespModel respModel = new RespModel();
                respModel.id = yxyHomeNews.getId();
                respModel.title = yxyHomeNews.getTitle();
                respModel.cover = ThumborUtil.resize(yxyHomeNews.getCover(), 300, 208);
                respModel.createDate = DateToolUtils.format(yxyHomeNews.getCreatedDate(), "yyyy-MM-dd");
                respModelLst.add(respModel);
            }
        }
        return respModelLst;
    }

    protected static class RepModel extends YxyApiReqModel {
        public String domain;

        public String schoolId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "域名发生错误，请刷新页面");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // 新闻id
        public String id;
        // 新闻标题
        public String title;
        // 新闻封面
        public String cover;

        public String createDate;
    }
}
